package com.android.tripspins.app.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.tripspins.app.MapActivity;
import com.android.tripspins.app.R;
import com.android.tripspins.app.bo.Pin;
import com.facebook.UiLifecycleHelper;

import java.io.File;
import java.util.ArrayList;

/**
 * Created by joaortk on 02/08/13.
 */
public class PinListAdapter  extends BaseAdapter {

    private Activity activity;
    private ArrayList<Pin> pins;
    private static LayoutInflater inflater = null;
    private int position;
    private UiLifecycleHelper uiLifecycleHelper;
    private long tripId;

    public PinListAdapter(Activity a, ArrayList<Pin> pins, long tripId, UiLifecycleHelper uiLifecycleHelper)
    {
        this.pins = pins;
        this.tripId = tripId;
        activity = a;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        position = 0;
        this.uiLifecycleHelper = uiLifecycleHelper;
    }

    @Override
    public int getCount() {
        return pins.size();
    }

    @Override
    public Object getItem(int i) {
        return pins.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        View v = view;
        if(view == null)
            v = inflater.inflate(R.layout.pin_list_row,null);

        v.setClickable(true);
        v.setFocusable(true);

        TextView pinRowTitle = (TextView)v.findViewById(R.id.pin_row_title);
        TextView pinRowDate = (TextView)v.findViewById(R.id.pin_row_date);
        TextView pinRowLocation = (TextView)v.findViewById(R.id.pin_row_location);
        TextView pinRowAttCount = (TextView)v.findViewById(R.id.pin_row_att_count);
        TextView pinRowAuxId = (TextView)v.findViewById(R.id.pin_row_id_aux);
        ImageView pinRowWeatherIcon = (ImageView)v.findViewById(R.id.pin_row_weather_icon);
        RelativeLayout pinRow = (RelativeLayout)v.findViewById(R.id.pin_row);
        //ImageView pinRowImgBackground = (ImageView)v.findViewById(R.id.pin_row);

        pinRowTitle.setText(pins.get(i).getTitle());
        pinRowDate.setText(pins.get(i).getDateFormatted());
        pinRowLocation.setText((pins.get(i).getAddress()==null? "-" : pins.get(i).getAddressString()));
        pinRowAttCount.setText(String.valueOf(pins.get(i).getAttachments().size()));
        pinRowAuxId.setText(String.valueOf(i));
        File weatherIcon =pins.get(i).getWeatherIconUri(activity,tripId);
        if(weatherIcon!=null)
        {
            pinRowWeatherIcon.setImageURI(Uri.fromFile(weatherIcon));
        }

        /*if (Build.VERSION.SDK_INT >= 16)
            pinRow.setBackground (BitmapDrawable.createFromPath(pins.get(i).getRandomImagePath()));
        else
            pinRow.setBackgroundDrawable (BitmapDrawable.createFromPath(pins.get(i).getRandomImagePath()));
        */






        //HorizontalScrollView scrollAttachments = (HorizontalScrollView)v.findViewById(R.id.pin_row_att_scroll);
        //BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
        //bitmapOptions.inPurgeable = true;
        //bitmapOptions.inInputShareable = true;
        //for(Attachment att : pins.get(i).getAttachments())
        //{
        //    ImageButton image = new ImageButton(activity);
        //    image.setImageURI(Uri.parse(att.getThumbnailPath()));
            //scrollAttachments.addView(image);
        //}

        v.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int index = Integer.parseInt(((TextView) view.findViewById(R.id.pin_row_id_aux)).getText().toString());
                //if(pins.get(index).getAttachments().size() > 0){
                    Intent intent = new Intent(activity, MapActivity.class);
                    intent.putExtra("pin", pins.get(index));
                    intent.putExtra("tripId", tripId);
                    activity.startActivity(intent);
                //}
            }
        });
        return v;
    }
}
